package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.ResourceMapper;
import com.qingcheng.dao.ResourceRoleIdsMapper;
import com.qingcheng.dao.RoleMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.system.Resource;
import com.qingcheng.pojo.system.ResourceRoleIds;
import com.qingcheng.pojo.system.ResourceRoleResult;
import com.qingcheng.pojo.system.Role;
import com.qingcheng.service.system.RoleService;
import com.sun.org.apache.bcel.internal.generic.INEG;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private ResourceRoleIdsMapper resourceRoleIdsMapper;

    @Autowired
    private ResourceMapper resourceMapper;

    /**
     * 返回全部记录
     *
     * @return
     */
    public List<Role> findAll() {
        return roleMapper.selectAll();
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Role> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<Role> roles = (Page<Role>) roleMapper.selectAll();
        return new PageResult<Role>(roles.getTotal(), roles.getResult());
    }

    /**
     * 条件查询
     *
     * @param searchMap 查询条件
     * @return
     */
    public List<Role> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return roleMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Role> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        Page<Role> roles = (Page<Role>) roleMapper.selectByExample(example);
        return new PageResult<Role>(roles.getTotal(), roles.getResult());
    }

    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    public ResourceRoleResult findByRoleId(Integer id) {
        //根据角色ID查找出所有 角色和权限ID 循环得出所有resourceId加到一个数组
      /*Example example = new Example(ResourceRoleIds.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleId", id);
        List<ResourceRoleIds> resourceRoleIds = resourceRoleIdsMapper.selectByExample(example);
        //根据ID查询出所有Role的

        List<Integer> resourceIds=new ArrayList<>();
        for (ResourceRoleIds resourceRoleId : resourceRoleIds) {
            Integer resourceId = resourceRoleId.getResourceId();
            resourceIds.add(resourceId);
        }
        List<Resource> resources = new ArrayList<>();
        Example example1 = new Example(Resource.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("id",resourceIds);*/
        ResourceRoleResult resourceRoleResult = new ResourceRoleResult();
        //根据查询Role对象
        Role role = roleMapper.selectByPrimaryKey(id);
        resourceRoleResult.setRole(role);
        //2根据ID查询权限集合对象
        List<Resource> resources = resourceMapper.findByRoleId(id);
        resourceRoleResult.setResources(resources);
        return resourceRoleResult;
    }

    /**
     * 修改
     *
     * @param resourceRoleResult
     */
    public void update(ResourceRoleResult resourceRoleResult) {
        //根据roleid先删除中间表再增加
       /* Integer roleId = resourceRoleResult.getRoleId();
        Example example = new Example(ResourceRoleIds.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleId", roleId);
        resourceRoleIdsMapper.deleteByExample(example);
        //定义一个新的中间表对象往里添加
        ResourceRoleIds resourceRoleIds = new ResourceRoleIds();
        List<Resource> resources = resourceRoleResult.getResources();
        List<Integer> resourceIds = new ArrayList<>();
        for (Resource resource : resources) {
            Integer resourceId = resource.getId();
            resourceIds.add(resourceId);
        }
        for (Integer resourceId : resourceIds) {
            resourceRoleIds.setRoleId(roleId);
            resourceRoleIds.setResourceId(resourceId);
            resourceRoleIdsMapper.insert(resourceRoleIds);
        }*/
        //1先修改role
        roleMapper.updateByPrimaryKeySelective(resourceRoleResult.getRole());
        Role role = resourceRoleResult.getRole();
        //先删中间表,再增加
        Example example = new Example(ResourceRoleIds.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleId", role.getId());
        resourceRoleIdsMapper.deleteByExample(example);
        List<Resource> resources = resourceRoleResult.getResources();
        ResourceRoleIds resourceRoleIds = new ResourceRoleIds();
        for (Resource resource : resources) {
            resourceRoleIds.setRoleId(role.getId());
            resourceRoleIds.setResourceId(resource.getId());
            resourceRoleIdsMapper.insert(resourceRoleIds);
        }
    }

    /**
     * 增加
     *
     * @param role
     */
    public void add(Role role) {
        roleMapper.insert(role);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void delete(Integer id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询条件
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Role.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 角色名称
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }

            // ID
            if (searchMap.get("id") != null) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }

        }
        return example;
    }

}
